// Package routers @APIVersion 1.0.0
// @Title fly API
// @Description fly接口文档
// @Contact xx@gmail.com
// @TermsOfServiceUrl http://xxx.me/
// @License Apache 2.0
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
package routers

import (
	"fly/controllers"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
)

// AuthWhiteList 鉴权白名单，支持正则
var AuthWhiteList = []string{"^.*/login$"}

func init() {
	// 用于多次打印请求响应日志
	beego.BConfig.CopyRequestBody = true

	beego.Router("/*", &controllers.MainController{})
	ns := beego.NewNamespace("/api",
		beego.NSNamespace("/biz/config", beego.NSInclude(&controllers.BizConfigController{})),
		beego.NSNamespace("/sys/dept", beego.NSInclude(&controllers.SysDeptController{})),
		beego.NSNamespace("/sys/menu", beego.NSInclude(&controllers.SysMenuController{})),
		beego.NSNamespace("/sys/role", beego.NSInclude(&controllers.SysRoleController{})),
		beego.NSNamespace("/sys/user", beego.NSInclude(&controllers.SysUserController{})),
		beego.NSNamespace("/sys/dictionary", beego.NSInclude(&controllers.SysDictController{})),
		beego.NSNamespace("/sys/log", beego.NSInclude(&controllers.SysLogAdminController{})),
		beego.NSNamespace("/sys/login/log", beego.NSInclude(&controllers.SysLogAdminLoginController{})),
		beego.NSNamespace("/airport", beego.NSInclude(&controllers.AirportController{})),
		beego.NSNamespace("/aircraft", beego.NSInclude(&controllers.AircraftController{})),
		beego.NSNamespace("/lifetime", beego.NSInclude(&controllers.LifetimeController{})),
		beego.NSNamespace("/component", beego.NSInclude(&controllers.AircraftComponentController{})),
		beego.NSNamespace("/location", beego.NSInclude(&controllers.AircraftLocationController{})),
		beego.NSNamespace("/task", beego.NSInclude(&controllers.FlightTaskController{})),
	)
	beego.AddNamespace(ns)

	// config swagger path
	beego.SetStaticPath("/swagger", "swagger")
	// add filter
	beego.InsertFilter("/api/*", beego.BeforeRouter, AddRequestMsgId)
	beego.InsertFilter("/api/*", beego.FinishRouter, ApiLogPrinter, beego.WithReturnOnOutput(false))
	beego.InsertFilter("/api/*", beego.FinishRouter, ApiLogRecord, beego.WithReturnOnOutput(false))
	// register error
	beego.ErrorController(&controllers.ErrorController{})
	beego.BConfig.RecoverFunc = controllers.Error500Handle
	// auth filter
	beego.InsertFilterChain("/api/*", func(next beego.FilterFunc) beego.FilterFunc {
		return func(ctx *context.Context) {
			if CheckAuth(ctx) {
				next(ctx)
			}
		}
	})
	//print router
	//tree := beego.PrintTree()
	//methods := tree["Content"].(beego.M)
	//for k, v := range methods {
	//	logs.Info("router: %s => %v", k, v)
	//}
}
